package com.sicnu.demo07.model

class Card(private var suit: String, private var rank: String, var isChosen:Boolean = false, var isMatched:Boolean = false)  {
    companion object {
        val rankStrings= arrayOf("A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K")
        val validSuits = arrayOf("♥", "♦", "♠", "♣")
    }

    override fun toString(): String {
        return suit + rank
    }

    fun match(otherCards: Array<Card>): Int {
        var score = 0
        when (otherCards.size) {
            1 -> {
                // 2张牌匹配逻辑
                val otherCard = otherCards[0]
                if (otherCard.rank == rank) {
                    score = 4
                } else if (otherCard.suit == suit) {
                    score = 1
                }
            }
            2 -> {
                // 3张牌匹配逻辑：三张牌点数相同得分最高，两张牌点数相同次之，三张牌花色相同再次之
                val card1 = otherCards[0]
                val card2 = otherCards[1]
                
                // 三张牌点数都相同
                if (this.rank == card1.rank && this.rank == card2.rank) {
                    score = 16
                }
                // 两张牌点数相同
                else if (this.rank == card1.rank || this.rank == card2.rank || card1.rank == card2.rank) {
                    score = 8
                }
                // 三张牌花色都相同
                else if (this.suit == card1.suit && this.suit == card2.suit) {
                    score = 4
                }
            }
        }
        return score
    }
}